package com.hanlin.backend.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

/**
 * @author:hl.yuan
 * @date:2021-01-12
 */
@Data
@ToString
@ApiModel(value = "分页对象")
public class PageCommon<T> {

    @ApiModelProperty("当前分页页数")
    private Long pageIndex = 1L;

    @ApiModelProperty("一页的数量")
    private Long pageSize = 20L;

    /**
     * 获取IPage对象，用于分页。默认：pageIndex = 1  pageSize = 20
     *
     * @return
     */
    public IPage<T> buildPage() {
        return new Page<>(this.getPageIndex(), this.getPageSize());
    }

    /**
     * 获取IPage对象，用于分页。手动传入分页参数
     *
     * @return
     */
    public IPage<T> buildPage(Long pageIndex, Long pageSize) {
        return new Page<>(pageIndex, pageSize);
    }

    /**
     * 用与table列表返回数据使用
     *
     * @param page
     * @return
     */
    public ResultCommon<TableCommon<T>> build(IPage<T> page) {
        if (page != null) {
            ResultCommon<TableCommon<T>> resultCommon = new ResultCommon<>();
            TableCommon<T> tableCommon = new TableCommon<>();
            tableCommon.setList(page.getRecords());
            tableCommon.setTotal(Long.valueOf(page.getRecords().size()));
            resultCommon.setData(tableCommon);
            return resultCommon;
        }
        return new ResultCommon();
    }

}
